<!-- 聊天列表 -->
<template>
    <div>
        <div class="conversationlist" style="height: 650px">
            <ul id="main" style="padding: 0;margin: 0">
                <li class="sessionlist" v-for="(item,index) in list" :key="index"
                    @contextmenu.prevent="clickAttribute(index)"
                    :class="{active: index === choiceIndex,activeRight: index === choiceRightIndex}"
                    @click="goToConversationPage(index)">
                    <div class="list-left">
                        <el-avatar shape="square" style="width: 50px;height: 50px" :src="item.url" @error="true"
                                   onerror="this.src='static/images/vue.jpg'">
                            <img src="static/images/vue.jpg"/>
                        </el-avatar>
                    </div>
                    <div class="list-right">
                        <div class="title-info">
                            <p class="name">{{item.name ? item.name : ''}}</p>
                            <span class="time">{{item.timestamp}}</span>
                        </div>
                        <div class="lastmsg-info">
                            <p class="lastmsg">{{item.lastmsg}}</p>
                            <span v-if="item.unread && item.unread > 0" class="unread-num">
                            <span class="unread-num-show">{{item.unread ? item.unread : 0}}</span>
                        </span>
                        </div>
                    </div>
                </li>
            </ul>
        </div>
        <!--分割线-->
        <el-divider direction="horizontal"></el-divider>

        <!--    右键菜单配置    -->
        <context-menu class="right-menu"
                      :target="contextMenuTarget"
                      :show="contextMenuVisible"
                      @update:show="(show) => update(show)">
            <a href="javascript:;" @click="overhead">顶置</a>
            <a href="javascript:;" @click="noDisturb">消息免打扰</a>
            <a href="javascript:;" @click="deleteConversation">删除</a>
        </context-menu>
    </div>
</template>

<script>
  export default {
    name: 'conversation.vue',
    data () {
      return {
        contextMenuTarget: '', // 绑定的dom
        contextMenuVisible: false,
        list: [
          {
            url: '',
            name: '开黑五人组',
            timestamp: '刚刚',
            lastmsg: '开黑了，兄弟们',
            unread: 31
          }, {
            url: '',
            name: 'Vue.js交流群',
            timestamp: '昨天',
            lastmsg: '你们知道怎么使用全局定义吗？',
            unread: 10
          }, {
            url: '',
            name: 'FreeIm官方交流群',
            timestamp: '昨天',
            lastmsg: '很好的软件',
            unread: 1
          }, {
            url: '',
            name: 'FreeIm官方交流1群',
            timestamp: '昨天',
            lastmsg: '不错的软件',
            unread: 4
          }, {
            url: '',
            name: 'FreeIm官方交流2群',
            timestamp: '昨天',
            lastmsg: '很好',
            unread: 41
          }, {
            url: '',
            name: 'FreeIm官方交流3群',
            timestamp: '昨天',
            lastmsg: '在吗?',
            unread: 3
          }, {
            url: '',
            name: 'FreeIm官方交流4群',
            timestamp: '昨天',
            lastmsg: '好的',
            unread: 5
          }
        ],
        choiceIndex: -1,
        choiceRightIndex: -1
      }
    },
    computed: {},
    mounted () {
      // 右击显示菜单 区域位置
      this.contextMenuTarget = document.getElementById('main')
    },
    methods: {
      goToConversationPage (index) {
        this.choiceIndex = index
        console.log('跳转到聊天页面')
      },
      noDisturb () {
        console.log('将回话' + this.choiceRightIndex + '设置为免打扰')
        this.update(false)
      },
      overhead () {
        console.log('将回话' + this.choiceRightIndex + '顶置')
        this.update(false)
      },
      deleteConversation () {
        console.log('将回话' + this.choiceRightIndex + '删除')
        this.update(false)
      },
      update (show) {
        this.contextMenuVisible = show
        if (!show) {
          this.choiceRightIndex = -1
        }
      },
      clickAttribute (index) {
        console.log('点击了右键' + index)
        this.contextMenuVisible = true
        this.choiceRightIndex = index
      }
    }
  }
</script>

<style scoped>
    .conversationlist {
        overflow-y: auto;
        overflow-x: hidden;
        box-sizing: border-box;
    }

    .sessionlist {
        display: flex;
        font-size: 0;
        border: 2px solid #f2f2f2;
    }

    .list-left {
        margin: 5px;
    }

    .list-right {
        position: relative;
        flex: 1;
        margin: 5px;
    }

    .title-info {
        display: flex;
    }

    .name {
        margin: 0;
        flex: 1 1 auto;
        font-size: 14px;
        overflow: hidden;
        white-space: nowrap;
        text-overflow: ellipsis;
    }

    .time {
        flex: 0 0 auto;
        float: right;
        line-height: 15px;
        color: #999;
        font-size: 10px;
        vertical-align: center;
        text-align: center;
    }

    .lastmsg-info {
        display: flex;
        margin-top: 13px;
    }

    .lastmsg {
        margin: 0;
        flex: 1 1 auto;
        font-size: 10px;
        height: 15px;
        line-height: 15px;
        color: #999;
        bottom: 4px;
        overflow: hidden;
        white-space: nowrap;
        text-overflow: ellipsis;
    }

    .unread-num {
        flex: 0 0 auto;
        margin-bottom: 0;
        display: inline-block;
        min-width: 16px;
        height: 16px;
        background-color: red;
        border-radius: 8px;
        font-size: 12px;
        color: #fff;
        line-height: 16px;
    }

    .unread-num-show {
        text-align: center;
        font-size: 10px;
        -webkit-transform: scale(0.8);
        display: block;
    }

    .el-divider--horizontal {
        margin: 0;
    }

    .active {
        background-color: #c4c4c4;
    }

    .activeRight {
        border: 2px solid #c4c4c4;
    }

    .right-menu {
        border: 1px solid #eee;
        box-shadow: 0 0.5em 1em 0 rgba(0, 0, 0, .1);
        border-radius: 1px;
        font-family: Microsoft Yahei, Avenir, Helvetica, Arial, sans-serif;
        -webkit-font-smoothing: antialiased;
        -moz-osx-font-smoothing: grayscale;
        text-align: center;
        color: #2c3e50;
        position: fixed;
        background: #fff;
        z-index: 999;
    }

    .right-menu a {
        padding: 2px 15px;
        height: 28px;
        line-height: 28px;
        text-align: center;
        display: block;
        color: #1a1a1a !important;
    }

    .right-menu a:-webkit-any-link {
        color: -webkit-link;
        cursor: pointer;
        text-decoration: none;
    }

    .right-menu a:hover {
        background: #42b983;
        color: #fff !important;
    }
</style>
